import 'package:get/get_connect/http/src/response/response.dart';
import 'package:kkhousekeep/models/login_owner_entity.dart';
import 'package:kkhousekeep/models/login_send_code.dart';
import 'package:kkhousekeep/net/base_connect.dart';
import 'package:kkhousekeep/tools/dialog_util.dart';

import '../models/login_code_entity.dart';
import '../models/login_property_entity.dart';
import 'api.dart';

class LoginConnect extends BaseConnect {
  Future<LoginOwnerEntity?> loginWithOwner(String phone, String code) async {
    var response =
        await post(Api.loginWithOwner, {"phone": phone, "code": code});
    if (response.body == null) {
      DialogUtil.showNetworkError();
      return Future.value();
    }
    return Future.value(LoginOwnerEntity.fromJson(response.body));
  }

  Future<LoginPropertyEntity?> loginByPwd(
      String username, String password) async {
    var response = await post(
        Api.loginByPwd, {"username": username, "password": password});
    if (response.body == null) {
      DialogUtil.showNetworkError();
      return Future.value();
    }
    return Future.value(LoginPropertyEntity.fromJson(response.body));
  }

  Future<LoginPropertyEntity?> loginByPhone(String phone, String code) async {
    var response = await post(Api.loginByPhone, {"phone": phone, "code": code});
    if (response.body == null) {
      DialogUtil.showNetworkError();
      return Future.value();
    }
    return Future.value(LoginPropertyEntity.fromJson(response.body));
  }

  Future<ForgetPwdCodeEntity?> resetPwdFirstStep(String phone) async {
    var response = await patch(Api.resetPwdFirstStep, {"phone": phone});
    if (response.body == null) {
      DialogUtil.showNetworkError();
      return Future.value();
    }
    return Future.value(
      ForgetPwdCodeEntity.fromJson(response.body),
    );
  }

  Future<ForgetPwdCodeEntity?> resetPwdSecondStep(
      String phone, String code) async {
    var response =
        await patch(Api.resetPwdSecondStep, {"phone": phone, "code": code});
    if (response.body == null) {
      DialogUtil.showNetworkError();
      return Future.value();
    }
    return Future.value(
      ForgetPwdCodeEntity.fromJson(response.body),
    );
  }

  Future<ForgetPwdCodeEntity?> resetPwdThirdStep(
      String phone, String password) async {
    var response = await patch(
        Api.resetPwdThirdStep, {"phone": phone, "password": password});
    if (response.body == null) {
      DialogUtil.showNetworkError();
      return Future.value();
    }
    return Future.value(
      ForgetPwdCodeEntity.fromJson(response.body),
    );
  }

  Future<SendCodeEntity?> sendCode(String phone, {bool isOwner = true}) async {
    Response response;
    if (isOwner) {
      response = await post(Api.sendCode, {"phone": phone});
    } else {
      response = await get(Api.sendCodeWithProperty, query: {"phone": phone});
    }
    if (response.body == null) {
      DialogUtil.showNetworkError();
      return Future.value();
    }
    return Future.value(SendCodeEntity.fromJson(response.body));
  }
}
